const Koa = require('koa');
const KoaRouter = require('koa-router');
const sendFile = require('koa-sendfile');
const path = require('path');
const cors = require('koa-cors');
const serve = require('koa-static');

const { startInterval, endInterval, getRedPocketRoomList, getAnchorPocketRoomList } = require('./src/tasks/rooms');

const app = new Koa();
const router = new KoaRouter();

app.use(cors({
  origin: '*',
  allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization'],
}));

app.use(serve('dist'));

router.get('/getRedPocketRoomList', async (ctx) => {
  const params = ctx.query;
  const res = await getRedPocketRoomList(params);
  ctx.body = res;
});

router.get('/getAnchorPocketRoomList', async (ctx) => {
  const params = ctx.query;
  const res = await getAnchorPocketRoomList(params);
  ctx.body = res;
});

router.get('/start', async (ctx) => {
  startInterval();
  ctx.body = {
    message: 'success'
  };
});

router.get('/end', async (ctx) => {
  endInterval();
  ctx.body = {
    message: 'success'
  };
});

app.use(router.allowedMethods()).use(router.routes());

module.exports = app;

startInterval();
